define(function(require, exports){
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var dspUtil = require('dspUtil');
	var why_Metrics = pubjs.config('defaultGroup/back/cols') || [];
	var con_Metrics = pubjs.config('defaultGroup/conversion/cols') || [];
	var Whisky_Metrics = why_Metrics.concat(con_Metrics);

	var all_id = '__all__';

	exports.base = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				uri: '',
				data: [],
				key: 'id',
				name: 'name',
				hasAll: false,
				singleSelect: false,
				changeFire: true,
				cls: 'M-labelSelect',
				tips: '',
				view_model: {
					data: [],
					key: 'id',
					name: 'name',
					vmClick: this.vmClick

				},
				allText: '全部'
			});

			this.Super('init', arguments);
		}
		,afterBuild: function()
		{
			var el = this.getDOM();
			var vm = this.vm.$;
			var c = this.getConfig();
			vm.id = c.id || 'id';
			vm.name = c.name || 'name';
			var data = util.clone(c.data);
			if(c.hasAll)
			{
				var head = {};
				head[vm.id] = all_id;
				head[vm.name] = c.allText;
				data.unshift(head);
			}

			this.formatData(data);
			vm.data = data;
			el.append(`
                <div class="${c.cls} fl">
                    <label ms-repeat-item="data" class="" ms-attr-title="{{item[name]}}" ms-class="act: item.selected" ms-click="vmClick(item)">
                        {{item[name]}}
                    </label>
                 </div>
			`);

			var config = {};
			if(c.tips)
			{
				el.append(
					` <span class="" pub-name="tip" pub-mod="@base/tip.desc"/>`
				);

				config.tip = {
					width: 300,
					data: c.tips
				};
			}

			this.buildFromTemplate(config)
		}
		,formatData: function(data)
		{
			util.each(data, function(item) {
				item.selected = false;
			});

			return data;
		}
		,vmClick: function(item)
		{
			var c = this.getConfig();
			var data = this.vm.$.data;
			var state = !item.selected;
			item.selected = state;
			if((item[c.key] == all_id && c.hasAll && item.selected) || c.singleSelect)
			{
				util.each(data, function(item) {
					if(item[c.key] != all_id)
					{
						item.selected = false;
					}
				});

				if(c.singleSelect)
				{
					item.selected = state;
				}
			}
			else if(item[c.key] != all_id && c.hasAll && item.selected && data[0].selected)
			{
				data[0].selected = false;
			}

			var value = this.getValue();
			if(c.maxCount && state)
			{
				if(value.length > c.maxCount)
				{
					item.selected = false;
					return pubjs.alert(LANG('最多只能选 %1 个', c.maxCount));
				}
			}

			if(c.changeFire)
			{
				this.fire('dataChange', this.getValue());
			}

			return false;
		}
		,getValue: function()
		{
			var c = this.getConfig();
			var data = util.clone(this.vm.$.data);
			var ret = [];
			var vm = this.vm.$;
			if(data && data[0] && data[0][vm.key] === all_id && data[0].selected)
			{
				return [all_id];
			}

			util.each(data, function(item, i) {
				if(item.selected)
				{
					ret.push(item[vm.key]);
				}
			});

			if(c.singleSelect)
			{
				return ret[0];
			}

			return ret.length ? ret : c.defaultValue || [];
		}
		,setValue: function(value)
		{
			if(value !== undefined)
			{
				if(!util.isArray(value))
				{
					value = [value];
				}

				var data = this.vm.$.data;
				var vm = this.vm.$;
				util.each(data, function(item) {
					if(value.indexOf(item[vm.key]) > -1)
					{
						item.selected = true;
					}
					else
					{
						item.selected = false;
					}
				});
			}

			return this;
		}
		,reset: function()
		{
			var data = this.vm.$.data;
			var vm = this.vm.$;
			util.each(data, function(item) {
				item.selected = false;
			});

			return this;
		}
		,disable: function(){
			this.getDOM().addClass('is-disabled');
			return this;
		},
		enable: function(){
			this.getDOM().removeClass('is-disabled');
			return this;
		},
	});

	exports.metricWithPop = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				changeFire: true,
				data: [],
				key: 'id',
				name: 'name',
				cls: 'M-labelWithPop',
				view_model: {
					data: [],
					key: 'id',
					name: 'name'
				}
			});

			this.Super('init', arguments);
		}
		,afterBuild: function()
		{
			var el = this.getDOM();
			var vm = this.vm.$;
			var c = this.getConfig();
			vm.id = c.id || 'id';
			vm.name = c.name || 'name';
			if(!c.data || !c.data.length)
			{
				c.data = c.defaultValue;
			}

			vm.data = util.clone(c.data);
			el.append(`
                <div class="${c.cls}">
                    <label ms-repeat-item="data" class="" ms-attr-title="{{item[name]}}">
                        {{item[name]}}
                    </label>
                 </div>

                 <span pub-name="go" pub-mod="@base/common/input.button"></span>
			`);

			this.buildFromTemplate({
				go: {
					'value': LANG('选择指标'),
					'class': 'btn btn-default'
				}
			});
		}
		,onInputClick: function(ev) {
			if(ev.name == 'go') {
				this.showPop(this.vm.$.data);
			}
		}
		,onMetricSelect: function(ev)
		{
			var c = this.getConfig();
			var value = ev.param || util.clone(this.getConfig('defaultValue'));
			if(this.$onlyWhisky)
			{
				this.clearMetricsByWhisky(value);
			}
			else
			{
				this.setData(value);
			}

			if(c.changeFire)
			{
				this.fire('dataChange', this.getValue());
			}

			return false;
		}
		,noWhisky: function()
		{
			this.$onlyWhisky = false;
			if(this.$.popMetric)
			{
				this.$.popMetric.showAll();
			}
		}
		,clearMetricsByWhisky: function(v)
		{
			var ret = [];
			var data = v || this.getValue();
			if(data && data.length)
			{
				for(var i = 0; i < data.length; i++)
				{
					if(~Whisky_Metrics.indexOf(data[i].id))
					{
						ret.push(data[i]);
					}
				}
			}

			this.$onlyWhisky = true;
			if(this.$.popMetric)
			{
				this.$.popMetric.onlyWhisky();
			}
			this.setData(ret);
			return this;
		}
		,setData: function(value)
		{
			this.vm.$.data = value || [];
			return this;
		}
		,getValue: function()
		{
			return this.vm.getItem('data');
		}
		,reset: function()
		{
			this.vm.reset();
			this.$onlyWhisky = null;
			return this;
		}
		,showPop: function(data)
		{
			var self = this;
			var mod = self.$.popMetric;
			if(!mod)
			{
				this.createAsync('popMetric',"modules/metricPanelPopwin.base", {
					singleSelect: false,
					onlyWhisky: self.$onlyWhisky,
					defaultValue: self.getConfig('defaultValue')
				},function(mod) {
					mod.reset().setData(data).show();
				});
			}
			else
			{
				mod.reset().setData(data).show();
			}
		}
	})
});